package com.lagou.dao;

import com.lagou.entity.OrderItem;
import com.lagou.entity.Orders;
import com.lagou.entity.Product;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersDao {

    //需求一 获取uid为001的用户的所有订单信息
    public List<Orders> findAllOrders(String uid) throws SQLException {

        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        String sql="select * from orders where uid=?";

        //一个用户所有的订单信息
        List<Orders> list = qr.query(sql, new BeanListHandler<Orders>(Orders.class), uid);

        return list;
    }


    //需求二 获取订单编号为order001的订单中的所有商品信息
    public List<Product> findOrderById(String oid) throws SQLException {

        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        //1.获取订单编号为order001的订单中的所有商品信息
        String sql="select pid from orderitem where oid=?";

        //2.查询的结果是 多条哦订单项数据
        List<OrderItem> list= qr.query(sql, new BeanListHandler<OrderItem>(OrderItem.class), oid);

        //3.创建集合保存商品信息
        List<Product> productList=new ArrayList<>();

        ProductDao productDao=new ProductDao();
        //4.遍历订单项集合 获取pid
        for(OrderItem orderItem:list){

            //4.1从orderitem中获取pid
            String pid=orderItem.getPid();

            //4.2调用productDao
            Product product = productDao.findProductById(pid);

            //4.3保存到集合
            productList.add(product);
        }

        //返回订单对应的商品信息
        return productList;
    }
}
